﻿using log4net;
using log4net.Config;
using log4net.Repository.Hierarchy;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 天离终端防火墙_专业版
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // 居中显示
            StartPosition = FormStartPosition.CenterScreen;
            // 关闭组件检查
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Tl_Warning.Invade_Warnig_Info open = new Tl_Warning.Invade_Warnig_Info();
            open.StartPosition = FormStartPosition.CenterScreen;
            open.ShowDialog();
            TL_Hanllder.TL_Log4net_Hanllder.WriteError("创建了预警");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 获取所有逻辑驱动器
            string[] drives = Directory.GetLogicalDrives();

            // 遍历每个驱动器
            foreach (string drive in drives)
            {
                try
                {
                    // 监视每个驱动器的根目录
                    MonitorDirectory(drive);
                    Console.WriteLine($"开始监视: {drive}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"无法监视 {drive}: {ex.Message}");
                }
            }

            // 防止程序结束
            Console.WriteLine("监视中... 按任意键退出");
            Console.ReadKey();
        }
        static void MonitorDirectory(string path)
        {
            // 创建FileSystemWatcher实例
            FileSystemWatcher watcher = new FileSystemWatcher
            {
                Path = path,
                Filter = "*.*",
                IncludeSubdirectories = true, // 包含子目录
                NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite
            };

            // 注册事件处理程序
            watcher.Created += OnFileCreatedOrChanged; // 监测文件创建
            watcher.Changed += OnFileCreatedOrChanged; // 监测文件修改

            // 启动监视
            watcher.EnableRaisingEvents = true;

            // 递归监视其他子目录
            foreach (string subDir in Directory.GetDirectories(path))
            {
                MonitorDirectory(subDir);
            }
        }

        // 处理文件创建和变更的事件
        private static void OnFileCreatedOrChanged(object sender, FileSystemEventArgs e)
        {
            // 排除 .temp, .TMP, .pma 文件以及新建的文件夹
            string extension = Path.GetExtension(e.FullPath);
            if (extension.Equals(".temp", StringComparison.OrdinalIgnoreCase) ||
                extension.Equals(".tmp", StringComparison.OrdinalIgnoreCase) ||
                extension.Equals(".pma", StringComparison.OrdinalIgnoreCase) ||
                Directory.Exists(e.FullPath)) // 检查是否是目录
            {
                return; // 不处理这些格式的文件或文件夹
            }

            // 检查文件内容
            CheckFileContentForKeyword(e.FullPath, "phpinfo");
        }

        // 检查文件内容是否包含关键词
        private static void CheckFileContentForKeyword(string filePath, string keyword)
        {
            try
            {
                // 读取文件内容
                string content = File.ReadAllText(filePath);

                // 检查是否包含关键词
                if (content.Contains(keyword))
                {
                    Console.WriteLine($"文件 {filePath} 包含关键词: {keyword}");
                }
            }
            catch (IOException ex)
            {
                Console.WriteLine($"无法读取文件 {filePath}: {ex.Message}");
            }
        }
        /// <summary>
        /// 拦截的规则库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 规则库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Tl_InterCept.规则库 open = new Tl_InterCept.规则库();
            //open.StartPosition = FormStartPosition.CenterScreen;
            //open.ShowDialog();
        }

        private void 高级配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TL_Settings.Senior open = new TL_Settings.Senior();
            open.StartPosition = FormStartPosition.CenterScreen;
            open.ShowDialog();
        }
    }
}
